package com.jieao.template.controller;

import cn.hutool.core.util.IdUtil;
import com.jieao.template.common.util.CurrentlyLoggedInUtil;
import com.jieao.template.common.util.JsonUtils;
import com.jieao.template.entity.accountMgr.AccountDO;
import com.jieao.template.entity.accountMgr.AssociatedRoleDTO;
import com.jieao.template.entity.accountMgr.PasswordDTO;
import com.jieao.template.entity.pub.JsonData;
import com.jieao.template.entity.pub.Pagination;
import com.jieao.template.entity.roleMgr.AssignmentPermissionDTO;
import com.jieao.template.service.AccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 账户表(Account)表控制层
 *
 * @author jzw
 * @since 2020-07-15 15:00:23
 */
@Slf4j
@Validated
@Api(value = "账户表模块接口", tags = "账户表模块接口")
@RestController
@RequestMapping("/api/v1/account")
public class AccountController
{
    /**
     * 服务对象
     */
    @Autowired
    private AccountService accountService;

    @ApiOperation(value = "修改密码", notes = "修改当前登录人密码")
    @PutMapping("accInfo")
    public JsonData setPassword(@RequestBody PasswordDTO passwordDTO)
    {
        log.info("修改当前登录人密码, 参数:{}, 操作人账户:{}", passwordDTO, CurrentlyLoggedInUtil.getAccount());
        return accountService.setPassword(passwordDTO);
    }

    @ApiOperation(value = "获取验证码", notes = "获取修改当前登录人密码的验证码")
    @GetMapping("verificationCode")
    public void verification(HttpServletResponse response) throws IOException
    {
        accountService.getVerification(response);
        log.info("获取修改当前登录人密码的验证码,  操作人账户:{}", CurrentlyLoggedInUtil.getAccount());
    }

    @ApiOperation(value = "给用户分配权限", notes = "给用户分配权限")
    @PostMapping("assignmentPermission")
    JsonData assignmentPermission(@RequestBody AssignmentPermissionDTO assignmentPermissionDTO)
    {
        accountService.assignmentPermission(assignmentPermissionDTO);
        log.info("给用户分配权限, 参数:{} , 操作人账户:{}", assignmentPermissionDTO, CurrentlyLoggedInUtil.getAccount());
        return JsonData.ok();
    }

    @PostMapping("associatedRole")
    @ApiOperation(value = "用户关联角色", notes = "用户关联角色")
    JsonData associatedRole(@RequestBody AssociatedRoleDTO associatedRoleDTO)
    {
        accountService.associatedRole(associatedRoleDTO);
        log.info("用户关联角色, 参数:{} , 操作人账户:{}", associatedRoleDTO, CurrentlyLoggedInUtil.getAccount());
        return JsonData.ok();
    }

    @ApiOperation(value = "获取当前用户已选的权限", notes = "获取当前用户已选的权限")
    @GetMapping("listCheckedPermission")
    JsonData listCheckedPermission(@ApiParam(name = "id", value = "用户id", required = true) String id)
    {
        log.info("获取当前用户已选的权限, 参数:{} , 操作人账户:{}", id, CurrentlyLoggedInUtil.getAccount());
        return JsonData.ok(accountService.listCheckedPermission(id));
    }

    @PatchMapping("disOrEnaAccount")
    @ApiOperation(value = "启禁用账户", notes = "启禁用账户")
    JsonData disOrEnaAccount(@ApiParam(name = "accountId", value = "账户id", required = true) @NotBlank(message = "账号id不能为空") String accountId)
    {
        log.info("启禁用账户, 参数:{} , 操作人账户:{}", accountId, CurrentlyLoggedInUtil.getAccount());
        accountService.disOrEnaAccount(accountId);
        return JsonData.ok();
    }

    @ApiOperation(value = "获取当前登录人名字和头像", notes = "获取当前你登录认名字和头像")
    @GetMapping("userName")
    JsonData getUserName()
    {
        log.info("获取当前登录人名字和头像, 操作人账户:{}", CurrentlyLoggedInUtil.getAccount());
        return JsonData.ok(accountService.getUserName());
    }

    @ApiOperation(value = "获取当前登录人菜单", notes = "获取当前登录人菜单")
    @GetMapping("menu")
    JsonData getMenuList()
    {
        log.info("获取当前登录人菜单, 操作人账户:{}", CurrentlyLoggedInUtil.getAccount());
        return JsonData.ok(accountService.getMenuList());
    }

    @GetMapping("/userInfo")
    @ApiOperation(value = "查询当前登录人信息", notes = "查询当前登录人信息")
    public JsonData userInfo()
    {
        log.info("查询当前登录人信息, 操作人账户:{}", CurrentlyLoggedInUtil.getAccount());
        return JsonData.ok(accountService.queryById(CurrentlyLoggedInUtil.getUserId()));
    }

    @GetMapping("/selectOne")
    @ApiOperation(value = "查询单条数据", notes = "通过主键查询Account表单条数据")
    public JsonData selectOne(@ApiParam(name = "id", value = "Account表id", required = true) @NotBlank(message = "id不能为空") String id)
    {
        log.info("通过id查询Account表单条数据, id:{}, 操作人账户:{}", id, CurrentlyLoggedInUtil.getAccount());
        return JsonData.ok(accountService.queryById(id));
    }

    /**
     * @author jzw
     * @since 2020-07-15 15:00:23
     */
    @GetMapping("/queryAll")
    @ApiOperation(value = "查询全部数据", notes = "可通过accountDO对象包含的参数进行数据筛选")
    public JsonData queryAll(@RequestParam Map<String, Object> params)
    {
        AccountDO account = JsonUtils.map2pojo(params, AccountDO.class);
        log.info("查询Account表数据, 参数:{}, 操作人账户:{}", account, CurrentlyLoggedInUtil.getAccount());
        return JsonData.ok(accountService.queryAll(account));
    }

    /**
     * @author jzw
     * @since 2020-07-15 15:00:23
     */
    @ApiOperation(value = "查询分页数据", notes = "可通过Pagination对象传递分页信息及其他参数进行数据筛选")
    @GetMapping("/queryPageList")
    public JsonData queryPageList(Pagination pagination)
    {
        log.info("分页查询Account表数据, 参数:{}, 操作人账户:{}", pagination, CurrentlyLoggedInUtil.getAccount());
        return JsonData.ok(accountService.queryAllByLimit(pagination));
    }

    /**
     * @author jzw
     * @since 2020-07-15 15:00:23
     */
    @ApiOperation(value = "新增数据", notes = "新增一条Account表数据")
    @PostMapping("/insert")
    public JsonData insert(@RequestBody AccountDO account)
    {
        account.setId(IdUtil.simpleUUID());
        accountService.insert(account);
        log.info("新增Account表数据, 参数:{}, 操作人账户:{}", account, CurrentlyLoggedInUtil.getAccount());
        return JsonData.ok();
    }

    /**
     * @author jzw
     * @since 2020-07-15 15:00:23
     */
    @ApiOperation(value = "修改数据", notes = "修改一条Account表数据")
    @PutMapping("/update")
    public JsonData update(@RequestBody AccountDO account)
    {
        accountService.update(account);
        log.info("修改Account表数据, 参数:{}, 操作人账户:{}", account, CurrentlyLoggedInUtil.getAccount());
        return JsonData.ok();
    }

    /**
     * @author jzw
     * @since 2020-07-15 15:00:23
     */
    @ApiOperation(value = "删除数据", notes = "通过主键假删除一条Account表数据")
    @DeleteMapping("/deleteById")
    public JsonData deleteById(@ApiParam(name = "id", value = "Account表id", required = true) String id)
    {
        accountService.deleteById(id);
        log.info("删除单条Account表数据, 参数:{}, 操作人账户:{}", id, CurrentlyLoggedInUtil.getAccount());
        return JsonData.ok();
    }

    /**
     * @author jzw
     * @since 2020-07-15 15:00:23
     */
    @ApiOperation(value = "批量删除数据", notes = "通过主键集合批量假删除Account表数据")
    @DeleteMapping("/batchDeletionByIdList")
    public JsonData batchDeletionByIdList(@RequestParam List<String> idList)
    {
        accountService.batchDeletionByIdList(idList);
        log.info("批量删除Account表数据, 参数:{}, 操作人账户:{}", idList, CurrentlyLoggedInUtil.getAccount());
        return JsonData.ok();
    }

}